/*
 * @Descripttion: 
 * @version: 
 * @Author: TT
 * @Date: 2023-10-21 17:01:08
 * @LastEditors: TT-hzy 
 * @LastEditTime: 2024-05-22 15:18:46
 */

import 'package:flutter/material.dart';
import 'package:hzy_project_template/config/config_index.dart';
import 'package:hzy_project_template/models/image_normal_model.dart';
import 'package:hzy_project_template/models/models_index.dart';
import 'package:hzy_project_template/utils/utils_index.dart';

import 'package:hzy_project_template/base_ui_config/base_ui_config_index.dart';

import '../../../commpontents/commpontents_index.dart';
import '../models/tu_chong_model.dart';
import '../tools/tu_chong_utils.dart';
import '../vm/tu_chong_state.dart';
import '../vm/tu_chong_vm.dart';
import '../widgets/example_tu_item_widget.dart';

class TuChongListV extends CommonGetXlistV<TuChongListC> {
  TuChongListV({super.key});

  @override
  TuChongListC get controller => Get.put(
        TuChongListC(),
      );

  @override
  Widget createListView(
    BuildContext context,
  ) {
    Widget body = GetBuilder<TuChongListC>(
      builder: (controller) {
        Widget bt = ListView.builder(
          itemBuilder: (context, index) {
            return ExampleTuItemlessV(
              tuChongItem: controller.state.feedList[index],
              tapOneKey: (tuChongItem) {
                controller.tapOneKey(
                  tuChongItem: tuChongItem,
                );
              },
            );
          },
          itemCount: controller.state.feedList.length,
        );

        return bt;
      },
    );
    return body;
  }
}

class TuChongListC extends CommonGetXListController {
  // --------- 属性 --------- //

  TuChongVM tuChongVM = TuChongVM();
  TuChongState state = TuChongState();
  // --------- 生命周期  --------- //

  // --------- 数据处理  --------- //

  @override
  configDefault() {
    super.configDefault();
    isShowAppBar = false;
    getNetWorkData(type: 3);
  }

  @override
  getNetWorkData({
    int? type,
    Map<String, dynamic>? info,
  }) async {
    state = await tuChongVM.configTuChongListData(
      page: page,
      isShowLoad: type == 3,
    );
    update();
    pageState.value = endRefresh(
      type: type ?? 1,
      state: state.pageState,
    );
  }

  @override
  tapPlaceHoldWidgetMethod({
    required CommonPlaceHoldType placeHoldType,
  }) {
    getNetWorkData(type: 3);
  }

  // --------- 触发事件  --------- //
  tapOneKey({
    required TuChongItem tuChongItem,
  }) {
    List<ImageNormalModel> imagelist = TuChongUtils.configDetailImageModelList(
      tuChongItem: tuChongItem,
      width: SizeMacro.screenWidth,
    );
    showToolTop(
      popType: 1,
      list: imagelist,
      tapCall: (index) async {
        if (judegUserIsLogin()) {
          if (index != 0) {
            configTapSelectImageTool(
              type: index,
              list: imagelist,
            );
          } else {
            tuChongVM.configAddCollect(
              tuChongItem: tuChongItem,
            );
          }
        }
      },
    );
  }
}
